package com.magicborrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.magicborrow.R;
import com.magicborrow.adapter.MyPageAdapter;
import com.magicborrow.fragment.BorrowingFragment;
import com.magicborrow.fragment.WantBorrowingFragment;
import com.magicborrow.utils.ViewPressedUtil;

import java.util.ArrayList;

public class FindActivity extends BaseActivity {
    private ViewPager pager;
    private ArrayList<Fragment> fragments;
    private MyPageAdapter adapter;
    private RadioGroup rgHappy;
    private RadioButton rb0;
    private RadioButton rb1;
    private TextView tvTag;
    private float x1;
    private float x2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find);
        initView();
        initData();
        findViewById(R.id.imb_search).setOnTouchListener(ViewPressedUtil.TouchPress);
        findViewById(R.id.imb_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到搜索页面
                Intent intent = new Intent(FindActivity.this,SearchActivity.class);
                startActivity(intent);
            }
        });
        FragmentManager manager = getSupportFragmentManager();
        adapter = new MyPageAdapter(manager, fragments);
        pager.setAdapter(adapter);
        initListener();


    }

    @Override
    public void onBackPressed() {
        this.getParent().onBackPressed();
    }

    private void initListener() {
        rgHappy.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (x1 == 0) {
                    x1 = rgHappy.getWidth() / 4 - tvTag.getWidth() / 2;
                    x2 = 3 * rgHappy.getWidth() / 4 - tvTag.getWidth() / 2;

                    tvTag.setX(x1);
                }
                //  ((RadioButton) findViewById(checkedId)).setChecked(true);
                pager.setCurrentItem(checkedId == rb0.getId() ? 0 : 1, true);
            }
        });
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (x1 == 0) {

                    x1 = rgHappy.getWidth() / 4 - tvTag.getWidth() / 2;
                    x2 = 3 * rgHappy.getWidth() / 4 - tvTag.getWidth() / 2;
                    tvTag.setX(x1);
                }
                switch (position) {
                    case 0:
                        tvTag.setX(x1 + (x2 - x1) * positionOffset);
                        break;
                    case 1:
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        rb0.setChecked(true);
                        break;
                    case 1:
                        rb1.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        rgHappy = (RadioGroup) findViewById(R.id.rg_happy);
        rb0 = (RadioButton) findViewById(R.id.rb_0);
        rb1 = (RadioButton) findViewById(R.id.rb_1);
        tvTag = (TextView) findViewById(R.id.tv_tag);
        pager = (ViewPager) findViewById(R.id.vp_happy);
    }

    private void initData() {
        fragments = new ArrayList<>();
        fragments.add(new BorrowingFragment());
        fragments.add(new WantBorrowingFragment() );
    }
}
